// pages/myPages/personalCenter/personalCenter.js
var request = require('./request.js')

//console.log(wallet)
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showView: false,
    userName: '',
    tempFilePaths: '',
    mobile_phone: '',
    setLang:{},
    showText:'',
    
  },

 
  userInfo: function (e) {
    var that = this;
    var data = {
      access_token: wx.getStorageSync('createUser').access_token
    };
    var t = wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'] : wx.T.locales['en']
    request.userInfo(data, function (d) {
      wx.setStorageSync('createUser', d)
      var photo=wx.getStorageSync('photo')
      if (photo == ''){
        var tempFile = d.head_portrait
      } else if (photo == that.data.tempFilePaths){
        var tempFile = photo
      }else{
        var tempFile = d.head_portrait
      }
      
      var showView = d.verified_status == 0 ? true : (d.verified_status == 1 ? false : false)
      if (d.verified_status == 0){
        that.data.show++
        if (that.data.show == 1 && showView == true){
          showView=true;
        }else{
          showView = false;
        }
      }
      that.setData({
        tempFilePaths: tempFile,
        userName: d.nickname,
        mobile_phone: d.mobile_phone,
        showText: d.verified_status == 0 ? t.GoToTheCertification:(d.verified_status == 1 ? t.InTheReview:t.GoToTheCertificationAlready),
        showView: showView
      })
    });
  },
  userNameInputEvent: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  onChangeShowState: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  realNameAuthentication:function(e){
    wx.navigateTo({
      url: './realNamePage/FillInInformation/FillInInformation?userN=' + this.data.userName+''
    })
    var that = this;
    if (that.data.showView){
      that.setData({
        showView: (!that.data.showView)
      })
    }
  },
  creditCard: function (e) {
    wx.navigateTo({
      url: './createReceipt/createReceipt'
    })
  },
  phoneNumber: function (e) {
    if (this.data.mobile_phone == ''){
      wx.navigateTo({
        url: './BindingCellPhoneNumber/BindingCellPhoneNumber'
      })
    }else{
      wx.navigateTo({
        url: './phoneNumber/phoneNumber?mobile_phone=' + this.data.mobile_phone + ''
      })
    } 
  },
  headSculpture: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['打开相机', '从相册选择'],
      success: function (res) {
        if (res.tapIndex === 0) {
          wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['camera'],
            success: function (res) {
              var tempFilePaths = res.tempFilePaths
              that.setData({
                tempFilePaths: res.tempFilePaths
              })
              
              wx.setStorage({ key: "photo", data: tempFilePaths[0] })
            },
          })
        } else if (res.tapIndex === 1) {
          wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album'],
            success: function (res) {
              console.log(res)
              var tempFilePaths = res.tempFilePaths
              that.setData({
                tempFilePaths: res.tempFilePaths
              })
              wx.setStorage({ key: "photo", data: tempFilePaths[0] })
            },
          })
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  }, 
  formSubmit: function (e) {
    var that = this;
    var photo = wx.getStorageSync('photo')
    //console.log(e)
    if (photo ==''){
      photo = 'http://tmp/wxa75be2a4e84344d6.o6zAJs6WEt_MmgaXrfzdW9KQyeCk.1gFjHqsE7c4M38809339c6d349483900babe24fe6688.png'
    }
    photo = this.data.tempFilePaths
    //console.log(photo)
       wx.uploadFile({
            url: 'https://wallet.kinlink.cn/api/member/info-update',
            header: { 'content-type': 'multipart/form-data' },
            method: 'POST',
            filePath: photo,
            name: 'head_portrait',
            formData: {
              'access_token': wx.getStorageSync('createUser').access_token,
              'nickname': that.data.userName,
            },
            success: function (res) {
              //.console.log(res)
              if (res.statusCode == 200) {
                wx.navigateBack({
                  delta: 1
                })
                wx.showToast({
                  title: '提交成功',
                  icon: 'succes',
                  duration: 500,
                  mask: true
                });
              }
            }
          })
    
  },
 
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      setLang: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'] : wx.T.locales['en']
    })
    wx.setNavigationBarTitle({
      title: wx.T.locale == "zh-Hans" ? wx.T.locales['zh-Hans'].PersonalCenter : wx.T.locales['en'].PersonalCenter
    })
    var that = this;
    that.userInfo();
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})